var totalPage =2;//一共多少页数据
	var pageSize =6;//每页显示10条数据
	var currentPage =1;//当前第几页数据，默认为1
	var isLoading = false;//是否正在加载更多数据//
	$(function(){
		//重置
		$('#btn-reset').click(function(){
			$('#search-orderNo').val("");
			$('#search-isFlag').val("");
			
		/*	$('#orderStatus').val("");*/
		});
		query(1);
		 function query(pageNum){
			 var p={};
			 p.pageSize = pageSize;
			 p.pageNum = pageNum;
			 $('#order-table').empty();
			 $('#order-table').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>订单编号</td><td>订单名称</td><td>总价</td><td>时间</td><td>状态</td></tr>');
			 
				$.ajax({
					  type:"POST",
					  url:"/movie.front/orderController/getOrderList",
					  data:JSON.stringify(p),
					  dataType:"json",
					  contentType:"application/json",
					  success:function(data){
			           array = data.result.list;
			           for(var i=0;i<array.length;i++){
			        	 var orderTime =    transfer(array[i].orderTime);
			        	   var html = '' ;
			        	  var   isFlag = array[i].isFlag;
			        	  var orderStatus = null;
			        	  if(isFlag==1){
			        		  orderStatus = "未支付";
			        	  }
			        	  if(isFlag==2){
			        		  orderStatus = "已失效";
			        	  }
			        	  if(isFlag==3){
			        		  orderStatus = "已支付";
			        	  }
			        	  if(isFlag==4){
			        		  orderStatus = "已退单";
			        	  }
			        		html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].orderNo+'</td><td>'+array[i].orderName+'</td><td>'+array[i].orderPrice+'</td><td>'+orderTime+'</td><td>'+orderStatus+'</td></tr>';
			           	    $('#order-table').append(html); 
			           }
					   //总共多少页
			           $('#paginator').empty();
			           var totalPages = data.result.pages;
			        	   var pageHtml = '<ul>';
			        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
			        	   for(var i=0;i<totalPages;i++){
			        		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
			        	   }
			        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
			        	   pageHtml =pageHtml+'</ul>';
			        	   $('#paginator').append(pageHtml);
					  }
				
				   });
		 }
		 $('#btn-search').click(function(){
			var orderNo = $('#search-orderNo')[0].value;
			var isFlag = $('#search-isFlag')[0].value;
		/*	var orderStatus=$('#orderStatus')[0].value;*/
			querySearch(1,orderNo,isFlag);
		 });
		 
		 function querySearch(pageNum,orderNo,isFlag){
			  var p={};
			  p.pageSize = pageSize;
			  p.pageNum = pageNum;
			  p.orderNo = orderNo;
			  p.isFlag = isFlag;
			  $('#order-table').empty();
				 $('#order-table').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>订单编号</td><td>订单名称</td><td>总价</td><td>时间</td><td>状态</td></tr>');
					$.ajax({
						  type:"POST",
						  url:"/movie.front/orderController/getOrderList",
						  data:JSON.stringify(p),
						  dataType:"json",
						  contentType:"application/json",
						  success:function(data){
				           array = data.result.list;
				           for(var i=0;i<array.length;i++){
				        		 var orderTime =  transfer(array[i].orderTime);
				        		 var   isFlag = array[i].isFlag;
					        	  var orderStatus = null;
					        	  if(isFlag==1){
					        		  orderStatus = "未支付";
					        	  }
					        	  if(isFlag==2){
					        		  orderStatus = "已失效";
					        	  }
					        	  if(isFlag==3){
					        		  orderStatus = "已支付";
					        	  }
					        	  if(isFlag==4){
					        		  orderStatus = "已退单";
					        	  }
				        	   var html = '' ;
				        	   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].orderNo+'</td><td>'+array[i].orderName+'</td><td>'+array[i].orderPrice+'</td><td>'+orderTime+'</td><td>'+orderStatus+'</td></tr>';
				           	    $('#order-table').append(html); 
				           }
						   //总共多少页
				           $('#paginator').empty();
				           var totalPages = data.result.pages;
				        	   var pageHtml = '<ul>';
				        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
				        	   for(var i=0;i<totalPages;i++){
				        		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
				        	   }
				        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
				        	   pageHtml =pageHtml+'</ul>';
				        	   $('#paginator').append(pageHtml);
						  }
					
					   });  
			 
		 }
			$('#btn-remove-order').click(function(){
				var list = new Array(); 
				var id=0;
				var array = $(("input[type='checkbox']"));
				for(var i=0;i<array.length;i++){
				  console.info(array[i].id);
				  if($('#'+array[i].id).is(':checked')){
				    	  var id = array[i].id;
				    	  list.push(id);
				    	
				  }
				}
				  deleteOrders(list);
			});
			
		 function deleteOrders(list){
			 var p ={};
			 p.list = list;
				$.ajax({
					  type:"POST",
					  url:"/movie.front/orderController/deleteOrders",
					  data:JSON.stringify(p),
					  dataType:"json",
					  contentType:"application/json",
					  success:function(data){
						  var count = data.result; 
						  if(count>0){
							  alert("删除成功");
								//刷新表格
				            	query(1);				  
						  }else{
							  alert("网络异常");
						  }
						  
						  
						 
						 
						  
					     }
				   });
		 }
	});
	
	
	//当前页
	function currentPagesquery(currentId){
		 // alert(currentId);当前页码
		  $('#paginator').empty();
		     var p ={};
	         p.pageSize = 6;
	         p.pageNum = currentId;
	         $('#order-table').empty();
			 $('#order-table').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>订单编号</td><td>订单名称</td><td>总价</td><td>时间</td><td>状态</td></tr>');
	    $.ajax({
		  type:"POST",
		  url:"/movie.front/orderController/getOrderList",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
	      array = data.result.list;
	      if(currentId==1){
	      for(var i=0;i<array.length;i++){
	    		 var orderTime =  transfer(array[i].orderTime);
	    	  var html = '' ;
	    	   var   isFlag = array[i].isFlag;
	        	  var orderStatus = null;
	        	  if(isFlag==1){
	        		  orderStatus = "未支付";
	        	  }
	        	  if(isFlag==2){
	        		  orderStatus = "已失效";
	        	  }
	        	  if(isFlag==3){
	        		  orderStatus = "已支付";
	        	  }
	        	  if(isFlag==4){
	        		  orderStatus = "已退单";
	        	  }
      		html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].orderNo+'</td><td>'+array[i].orderName+'</td><td>'+array[i].orderPrice+'</td><td>'+orderTime+'</td><td>'+orderStatus+'+<td></tr>';
         	    $('#order-table').append(html); 
	         }
	      }
	      else{
	    		
	    	   for(var i=0;i<array.length;i++){
	    		   var orderTime =  transfer(array[i].orderTime);
	    		   var flag = (currentId-1)*6+i+1;
	    		   var html = '' ;
	    		   var   isFlag = array[i].isFlag;
		        	  var orderStatus = null;
		        	  var flag = (currentId-1)*6+i+1;
		        	  if(isFlag==1){
		        		  orderStatus = "未支付";
		        	  }
		        	  if(isFlag==2){
		        		  orderStatus = "已失效";
		        	  }
		        	  if(isFlag==3){
		        		  orderStatus = "已支付";
		        	  }
		        	  if(isFlag==4){
		        		  orderStatus = "已退单";
		        	  }
	    		   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+flag+'</td><td>'+array[i].orderNo+'</td><td>'+array[i].orderName+'</td><td>'+array[i].orderPrice+'</td><td>'+orderTime+'</td><td>'+orderStatus+'</td></tr>';
	           	    $('#order-table').append(html); 
	    	         }
	      }
	      
		   //总共多少页
	      var totalPages = data.result.pages;
	   	   var pageHtml = '<ul>';
	   	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
	   	   for(var i=0;i<totalPages;i++){
	   		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
	   	   }
	   	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
	   	   pageHtml =pageHtml+'</ul>';
	   	   $('#paginator').append(pageHtml);
		  }

	  });
	}

	//首页
	function  firstPagesquery(firstPageId){
		currentPagesquery(firstPageId);
	}

	//尾页
	function  endPagequery(endPageId){
		currentPagesquery(endPageId);
		 }
	
	 
	   function transfer(val){
			if(val){
				val = parseInt(val, 10) * 1000;
				var date = new Date(val);
				//return date.format('yyyy-MM-dd hh:mm:ss');
				return date.format('yyyy-MM-dd hh:mm:00');
			}
			return '';
		}
		//重写javaScript日期对象原型
		Date.prototype.format = function(fmt){
			var o = {
				'M+': this.getMonth() + 1,//月份
				'd+': this.getDate(),//日
				'h+': this.getHours(),//小时
				'm+': this.getMinutes(),//分
				's+': this.getSeconds(),//秒
				'q+': Math.floor((this.getMonth() + 3) / 3),//季度
				'S': this.getMilliseconds()//毫秒
			};
			if(/(y+)/.test(fmt))
				fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
			for( var k in o)
				if(new RegExp('(' + k + ')').test(fmt))
					fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
			return fmt;
		}
	    
	